package com.youxue.teachin.user.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import com.Navigator;
import com.ali.music.navigator.backstack.IntentParam;
import com.youxue.teachin.BuildConfig;
import com.youxue.teachin.R;
import com.youxue.teachin.WechatUtils;
import com.youxue.teachin.common.utils.PreContent;
import com.youxue.teachin.common.utils.PreferencesUtils;
import com.youxue.teachin.common.utils.SchemaParam;
import com.youxue.teachin.common.utils.SchemaPath;
import com.youxue.teachin.user.login.presenter.LoginPresenter;
import com.youxue.teachin.user.userSystem.UserSystem;
import com.youxue.teachin.youmen.UMenUtils;
import com.youxue.teachin.youmen.UMenUtils.UmenConfig;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-04 10:06
 * @version: 9.1.0
 */
public class LoginActivity extends Activity implements OnClickListener, OnEditorActionListener, ILoginView {
    private View mLoginPhoneInput;
    private EditText mLoginInputPhone;
    private View mPasswordInput;
    private EditText mLoginPassword;
    private TextView mChangePassward;
    private ImageView mQq;
    private ImageView mWechat;
    private ImageView mWeibo;
    private TextView mNewUser;
    private TextView mLookAround;

    private View mLoginView;

    LoginPresenter mPresenter;
    private String loginProvide = "teachin";

    boolean isToLancher;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.user_login_activity);
        mPresenter = new LoginPresenter(this);

        isToLancher = getParam(getIntent()).getBoolean(SchemaParam.TOLAUNCHE, false);
        initView();
    }

    IntentParam getParam(Intent intent) {
        IntentParam param = new IntentParam(intent);
        return param;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent == null) {
            return;
        }
        isToLancher = getParam(intent).getBoolean(SchemaParam.TOLAUNCHE, false);

        String code = intent.getStringExtra("code");
        String state = intent.getStringExtra("state");
        if (!TextUtils.isEmpty(code)) {
            mPresenter.loginThreed(code, state);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    private void initView() {
        mLoginPhoneInput = findViewById(R.id.login_input_phone_include);
        mLoginInputPhone = (EditText)findViewById(R.id.login_input_phone);
        mPasswordInput = findViewById(R.id.login_password_include);
        mLoginPassword = (EditText)findViewById(R.id.login_password);
        mChangePassward = (TextView)findViewById(R.id.reset_passward);
        mQq = (ImageView)findViewById(R.id.qq);
        mWechat = (ImageView)findViewById(R.id.wechat);
        mWeibo = (ImageView)findViewById(R.id.weibo);
        mNewUser = (TextView)findViewById(R.id.new_user);
        mLookAround = (TextView)findViewById(R.id.look_around);

        mLoginView = findViewById(R.id.login_bt);

        mLoginInputPhone.setOnClickListener(this);
        mLoginPassword.setOnClickListener(this);
        mChangePassward.setOnClickListener(this);
        mQq.setOnClickListener(this);
        mWechat.setOnClickListener(this);
        mWeibo.setOnClickListener(this);
        mNewUser.setOnClickListener(this);
        mLookAround.setOnClickListener(this);
        mLoginView.setOnClickListener(this);

        mLoginInputPhone.setOnEditorActionListener(this);
        mLoginPassword.setOnEditorActionListener(this);

        mLoginPassword.setImeOptions(EditorInfo.IME_ACTION_SEND);

        if (BuildConfig.DEBUG) {
            mLoginInputPhone.setText("18501714348");
            mLoginPassword.setText("111111");
        }
    }

    private void login() {
        mPresenter.login(mLoginInputPhone.getText().toString(), mLoginPassword.getText().toString());
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.login_bt) {
            loginProvide = "teachin";
            login();
        } else if (id == R.id.qq) {
        } else if (id == R.id.weibo) {
        } else if (id == R.id.wechat) {
            WechatUtils.reqToWx(this.getApplicationContext());
            WechatUtils.wxLogin();
            loginProvide = "微博";
        } else if (id == R.id.reset_passward) {
            new Navigator.Builder()
                .setPageName("reset_mind_password")
                .build().open();
        } else if (id == R.id.new_user) {
            new Navigator
                .Builder()
                .setPageName("new_user")
                .build()
                .open();
        } else if (id == R.id.look_around) {
            PreferencesUtils.saveBoolean(PreContent.LOOK_AROUND, true);
            new Navigator.Builder()
                .setPageName(SchemaPath.PAGE_MAIN_LANCHER)
                .build().open();
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        int id = v.getId();
        if (id == R.id.login_input_phone) {
            mLoginPassword.requestFocus();
            return true;
        } else if (id == R.id.login_password) {
            login();
            return true;
        }
        return false;
    }

    @Override
    public void loginSuccess() {
        if (isToLancher) {
            new Navigator.Builder()
                .setPageName("main_lancher")
                .build().open();
        }
        PreferencesUtils.saveBoolean(PreContent.LOOK_AROUND, false);
        UserSystem.getInstance().sendLoginSuccess();
        UmenConfig umenConfig = new UmenConfig();
        umenConfig.ProfileSignInProvider = loginProvide;
        umenConfig.ProfileSignInId = String.valueOf(UserSystem.getInstance().getUserVO().id);
        UMenUtils.updateConfig(umenConfig);
        finish();
    }

    @Override
    public void loginFail() {

    }
}
